package controlleur.form;

import java.rmi.RemoteException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import annuaire.beans.Student;
import annuaire.exceptions.BusinessException;
import annuaire.rmi.remotedObject.IRemotedAnnuaire;
import annuaire.rmi.remotedObject.IRemotedUser;
import form.bean.StudentForm;

/**
 * Contrôleur responsable de la mise a jour d'un etudiant par lui-même
 * 
 * @author Yannick Lorenzati & Stephanie Malakian
 * 
 */
public class ModifyItSelfFormControlleur extends SimpleFormController {

	/** Logger pour cette classe ainsi que pour ses sous classes */
	protected final Log logger = LogFactory.getLog(getClass());

	/**
	 * Reference vers l'objet distant Annuaire
	 */
	private IRemotedAnnuaire remotedAnnuaire;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax
	 * .servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse,
	 * java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object commandAjout,
			BindException errors) throws Exception {

		StudentForm s = (StudentForm) commandAjout;
		IRemotedUser user = (IRemotedUser) request.getSession().getAttribute(
				"user");
		Student old = (Student) user.getBeans();

		// si le login a changé
		if (!old.getLogin().equals(s.getLogin())) {

			Student pattern = new Student();
			pattern.setLogin(s.getLogin());
			pattern = remotedAnnuaire.searchOneStudent(pattern);

			// si nouveau login deja pris
			if (pattern != null) {
				errors.rejectValue("login", "key", "Ce login est deja pris");
				return showForm(request, response, errors);
			}
		}

		// on fait l'update
		Student newStudent = old;
		newStudent.setAddress(s.getAddress());
		newStudent.setComments(s.getComments());
		newStudent.setFirstName(s.getFirstName());
		newStudent.setName(s.getName());
		newStudent.setLogin(s.getLogin());
		newStudent.setMail(s.getMail());
		newStudent.setWeb(s.getWeb());
		newStudent.setSociety(s.getSociety());
		user.updateBeans(newStudent);

		// MAJ de la session
		request.getSession().setAttribute("user", user);

		ModelAndView mav = showForm(request, response, errors);
		mav.addObject("succesStu", "Modification effectué avec succes");
		return mav;
		// return new ModelAndView(getSuccessView(), "model", myModel);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject
	 * (javax.servlet.http.HttpServletRequest)
	 */
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws ServletException {
		StudentForm studentForm = new StudentForm();
		try {
			Student s = (Student) ((IRemotedUser) request.getSession()
					.getAttribute("user")).getBeans();

			// on prerempli un max
			studentForm.setAddress(s.getAddress());
			studentForm.setComments(s.getComments());
			studentForm.setFirstName(s.getFirstName());
			studentForm.setLogin(s.getLogin());
			studentForm.setMail(s.getMail());
			studentForm.setName(s.getName());
			studentForm.setSociety(s.getSociety());
			studentForm.setWeb(s.getWeb());

		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (BusinessException e) {
			e.printStackTrace();
		}

		return studentForm;
	}

	/**
	 * @param remotedAnnuaire
	 */
	public void setRemotedAnnuaire(IRemotedAnnuaire remotedAnnuaire) {
		this.remotedAnnuaire = remotedAnnuaire;
	}

	/**
	 * @return
	 */
	public IRemotedAnnuaire getRemotedAnnuaire() {
		return remotedAnnuaire;
	}

}
